package com.smarthome.common.httpwapper;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.nio.charset.Charset;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

public class HttpMethod {
    
    public static String DEFAULT_CHARSET="UTF-8";
    
    public static String DEFAULT_CONTENT_TYPE="application/json;charset=UTF-8";
    
    public static String POST="POST";
    
    public static String GET="GET";
    
    private static RequestConfig requestConfig;
    
    static{
        requestConfig = RequestConfig.custom()
                        .setSocketTimeout(120*1000) //获取数据的超时时间
                        .setConnectTimeout(120*1000)//建立连接超时时间
                        .setConnectionRequestTimeout(120*1000) //从连接管理器中获取连接超时时间
                        .build();
    }
    
    /**
     * 组装HTTP GET请求对象
     * @param requestUrl  请求URL
     * @param headersMap  请求头键值对Map
     * @return {@link org.apache.http.client.methods.HttpGet}
     * @throws URISyntaxException
     */
    public static HttpGet buildGet(String requestUrl,Map<String,String> headersMap)
            throws URISyntaxException {
        HttpGet get = new HttpGet(requestUrl);
        get.setHeader(HttpHeaders.CONTENT_TYPE,DEFAULT_CONTENT_TYPE);
        //get.setHeader("Connection", "close"); 重用连接，客户端不主动关闭，由调用HttpGet释放连接
        get.setConfig(requestConfig);
        if(headersMap==null){
            return get;
        }
        Set<Entry<String,String>> headerEntrySet=headersMap.entrySet();
        for(Entry<String,String> headerEntry:headerEntrySet){
            get.setHeader(headerEntry.getKey(), headerEntry.getValue());
        }
        return get;
    }

    /**
     * 组装HTTP POST 请求对象
     * @param requestUrl 请求URL
     * @param headersMap 请求头键值对Map
     * @param content 提交的内容
     * @return {@link org.apache.http.client.methods.HttpPost}
     * @throws URISyntaxException
     * @throws UnsupportedEncodingException
     */
    public static HttpPost buildPost(String requestUrl,
                                     Map<String,String> headersMap,
                                     String content)
            throws URISyntaxException, UnsupportedEncodingException {
        
        HttpPost post = new HttpPost(requestUrl);
        post.setHeader(HttpHeaders.CONTENT_TYPE, DEFAULT_CONTENT_TYPE);
        //post.setHeader("Connection", "close");重用连接，客户端不主动关闭，由调用HttpGet释放连接
        post.setConfig(requestConfig);
        
        //处理中文乱码问题
        StringEntity bodyEntity = new StringEntity(content,Charset.forName(DEFAULT_CHARSET));
        bodyEntity.setContentType(DEFAULT_CONTENT_TYPE);
        
        post.setEntity(bodyEntity);        
        if(headersMap==null){
            return post;
        }
        
        Set<Entry<String,String>> headerEntrySet=headersMap.entrySet();
        for(Entry<String,String> headerEntry:headerEntrySet){
            post.setHeader(headerEntry.getKey(), headerEntry.getValue());
        }
        return post;
    }
}
